import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '../store/index'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('../views/index'),
    children: [
      {
        path: '/',
        name: '首页',
        component: () => import('../views/welcome')
      },
      // {
      //   path: '/sys/user',
      //   name: '用户管理',
      //   component: () => import('../views/admin/user/index')
      // },
      // {
      //   path: '/sys/menu',
      //   name: '菜单管理',
      //   component: () => import('../views/admin/menu/index')
      // },
      // {
      //   path: '/sys/dept',
      //   name: '部门管理',
      //   component: () => import('../views/admin/dept/index')
      // },
      // {
      //   path: '/sys/dict',
      //   name: '字典管理',
      //   component: () => import('../views/admin/dict/index')
      // },
      // {
      //   path: '/sys/log',
      //   name: '系统日志',
      //   component: () => import('../views/admin/log/index')
      // },
      // {
      //   path: '/sys/role',
      //   name: '角色管理',
      //   component: () => import('../views/admin/role/index')
      // },
      // {
      //   path: '/sys/file',
      //   name: '文件管理',
      //   component: () => import('../views/admin/file/index')
      // },
      {
        path: '/generator',
        name: '代码生成',
        component: () => import('../views/generator/index'),
      }, {
        path: '/generator/form',
        name: '表单设计',
        component: () => import('../views/generator/form'),
      },
      //

      {
        path: '/info/index',
        name: '个人信息',
        component: () => import('../views/admin/user/info')
      }
    ]
  },
  {
    path: '/login',
    name: '用户登录',
    component: () => import('../views/login')
  },
  {
    path: '/404',
    name: 'notFound',
    component: () => import('../components/error-page/404')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path.includes('http')) {
    // window.open(to.path.replace('http://localhost:8080/sys/', ''))

    let href = to.path.substring(to.path.indexOf('http'))
    console.log(to.path.substring(to.path.indexOf('http')))
    // window.location.href=href
    window.open(href)
  } else {
    NProgress.start()
    // 登录界面登录成功之后，会把用户信息保存在会话
    // 存在时间为会话生命周期，页面关闭即失效
    let user = sessionStorage.getItem('user')
    if (to.path === '/login') {
      // 如果是访问登录界面，如果用户会话信息存在，代表已登录过，跳转到主页
      if (user) {
        next({path: '/'})
      } else {
        next()
      }
    } else {
      // 如果访问非登录界面，且会话信息不存在，代表未登录，则跳转到登录界面
      if (!user) {
        next({path: '/login'})
      } else {
        // next()
        if (store.getters.roles.length === 0) {
          store.dispatch('GetUserInfo').then(() => {
            next()
          })
        } else {
          next()
        }

      }
    }
  }

})
router.afterEach((to, from, next) => {
  if (to.path.includes('http')) {
    // window.open(to.path)
  }
    else {
    if (to.path !== '/login') {
      let name = to.name
      let path = to.path
      // openedPath 为当前点击的列表项在openedTab中的key，若不存在则为undefined
      let openedTab = store.state.tabs.openedTab
      let openedPath = ''
      if (openedTab) {
        openedTab.forEach((tabs, index) => {
          if (tabs.name === name && tabs.path === path) {
            openedPath = tabs.path
          }
        })
      }
      if (openedPath === path) {
        // 该标签是已经打开过的，需要激活此标签页
        store.commit('changeTab', path)
      } else {
        // 该标签当前没有打开
        // 将name, path加入到已打开标签页state.openedTab图中
        store.commit('addTab', {name, path})
      }
    }
  }

  NProgress.done()

})
export default router
